home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c03 / AllOps.java next >
Encoding:
Text File  |  2000-05-25  |  10.0 KB  |  456 lines

  1. //: AllOps.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Tests all the operators on all the
  50. // primitive data types to show which
  51. // ones are accepted by the Java compiler.
  52.  
  53. class AllOps {
  54.   // To accept the results of a boolean test:
  55.   void f(boolean b) {}
  56.   void boolTest(boolean x, boolean y) {
  57.     // Arithmetic operators:
  58.     //! x = x * y;
  59.     //! x = x / y;
  60.     //! x = x % y;
  61.     //! x = x + y;
  62.     //! x = x - y;
  63.     //! x++;
  64.     //! x--;
  65.     //! x = +y;
  66.     //! x = -y;
  67.     // Relational and logical:
  68.     //! f(x > y);
  69.     //! f(x >= y);
  70.     //! f(x < y);
  71.     //! f(x <= y);
  72.     f(x == y);
  73.     f(x != y);
  74.     f(!y);
  75.     x = x && y;
  76.     x = x || y;
  77.     // Bitwise operators:
  78.     //! x = ~y;
  79.     x = x & y;
  80.     x = x | y;
  81.     x = x ^ y;
  82.     //! x = x << 1;
  83.     //! x = x >> 1;
  84.     //! x = x >>> 1;
  85.     // Compound assignment:
  86.     //! x += y;
  87.     //! x -= y;
  88.     //! x *= y;
  89.     //! x /= y;
  90.     //! x %= y;
  91.     //! x <<= 1;
  92.     //! x >>= 1;
  93.     //! x >>>= 1;
  94.     x &= y;
  95.     x ^= y;
  96.     x |= y;
  97.     // Casting:
  98.     //! char c = (char)x;
  99.     //! byte B = (byte)x;
  100.     //! short s = (short)x;
  101.     //! int i = (int)x;
  102.     //! long l = (long)x;
  103.     //! float f = (float)x;
  104.     //! double d = (double)x;
  105.   }
  106.   void charTest(char x, char y) {
  107.     // Arithmetic operators:
  108.     x = (char)(x * y);
  109.     x = (char)(x / y);
  110.     x = (char)(x % y);
  111.     x = (char)(x + y);
  112.     x = (char)(x - y);
  113.     x++;
  114.     x--;
  115.     x = (char)+y;
  116.     x = (char)-y;
  117.     // Relational and logical:
  118.     f(x > y);
  119.     f(x >= y);
  120.     f(x < y);
  121.     f(x <= y);
  122.     f(x == y);
  123.     f(x != y);
  124.     //! f(!x);
  125.     //! f(x && y);
  126.     //! f(x || y);
  127.     // Bitwise operators:
  128.     x= (char)~y;
  129.     x = (char)(x & y);
  130.     x  = (char)(x | y);
  131.     x = (char)(x ^ y);
  132.     x = (char)(x << 1);
  133.     x = (char)(x >> 1);
  134.     x = (char)(x >>> 1);
  135.     // Compound assignment:
  136.     x += y;
  137.     x -= y;
  138.     x *= y;
  139.     x /= y;
  140.     x %= y;
  141.     x <<= 1;
  142.     x >>= 1;
  143.     x >>>= 1;
  144.     x &= y;
  145.     x ^= y;
  146.     x |= y;
  147.     // Casting:
  148.     //! boolean b = (boolean)x;
  149.     byte B = (byte)x;
  150.     short s = (short)x;
  151.     int i = (int)x;
  152.     long l = (long)x;
  153.     float f = (float)x;
  154.     double d = (double)x;
  155.   }
  156.   void byteTest(byte x, byte y) {
  157.     // Arithmetic operators:
  158.     x = (byte)(x* y);
  159.     x = (byte)(x / y);
  160.     x = (byte)(x % y);
  161.     x = (byte)(x + y);
  162.     x = (byte)(x - y);
  163.     x++;
  164.     x--;
  165.     x = (byte)+ y;
  166.     x = (byte)- y;
  167.     // Relational and logical:
  168.     f(x > y);
  169.     f(x >= y);
  170.     f(x < y);
  171.     f(x <= y);
  172.     f(x == y);
  173.     f(x != y);
  174.     //! f(!x);
  175.     //! f(x && y);
  176.     //! f(x || y);
  177.     // Bitwise operators:
  178.     x = (byte)~y;
  179.     x = (byte)(x & y);
  180.     x = (byte)(x | y);
  181.     x = (byte)(x ^ y);
  182.     x = (byte)(x << 1);
  183.     x = (byte)(x >> 1);
  184.     x = (byte)(x >>> 1);
  185.     // Compound assignment:
  186.     x += y;
  187.     x -= y;
  188.     x *= y;
  189.     x /= y;
  190.     x %= y;
  191.     x <<= 1;
  192.     x >>= 1;
  193.     x >>>= 1;
  194.     x &= y;
  195.     x ^= y;
  196.     x |= y;
  197.     // Casting:
  198.     //! boolean b = (boolean)x;
  199.     char c = (char)x;
  200.     short s = (short)x;
  201.     int i = (int)x;
  202.     long l = (long)x;
  203.     float f = (float)x;
  204.     double d = (double)x;
  205.   }
  206.   void shortTest(short x, short y) {
  207.     // Arithmetic operators:
  208.     x = (short)(x * y);
  209.     x = (short)(x / y);
  210.     x = (short)(x % y);
  211.     x = (short)(x + y);
  212.     x = (short)(x - y);
  213.     x++;
  214.     x--;
  215.     x = (short)+y;
  216.     x = (short)-y;
  217.     // Relational and logical:
  218.     f(x > y);
  219.     f(x >= y);
  220.     f(x < y);
  221.     f(x <= y);
  222.     f(x == y);
  223.     f(x != y);
  224.     //! f(!x);
  225.     //! f(x && y);
  226.     //! f(x || y);
  227.     // Bitwise operators:
  228.     x = (short)~y;
  229.     x = (short)(x & y);
  230.     x = (short)(x | y);
  231.     x = (short)(x ^ y);
  232.     x = (short)(x << 1);
  233.     x = (short)(x >> 1);
  234.     x = (short)(x >>> 1);
  235.     // Compound assignment:
  236.     x += y;
  237.     x -= y;
  238.     x *= y;
  239.     x /= y;
  240.     x %= y;
  241.     x <<= 1;
  242.     x >>= 1;
  243.     x >>>= 1;
  244.     x &= y;
  245.     x ^= y;
  246.     x |= y;
  247.     // Casting:
  248.     //! boolean b = (boolean)x;
  249.     char c = (char)x;
  250.     byte B = (byte)x;
  251.     int i = (int)x;
  252.     long l = (long)x;
  253.     float f = (float)x;
  254.     double d = (double)x;
  255.   }
  256.   void intTest(int x, int y) {
  257.     // Arithmetic operators:
  258.     x = x * y;
  259.     x = x / y;
  260.     x = x % y;
  261.     x = x + y;
  262.     x = x - y;
  263.     x++;
  264.     x--;
  265.     x = +y;
  266.     x = -y;
  267.     // Relational and logical:
  268.     f(x > y);
  269.     f(x >= y);
  270.     f(x < y);
  271.     f(x <= y);
  272.     f(x == y);
  273.     f(x != y);
  274.     //! f(!x);
  275.     //! f(x && y);
  276.     //! f(x || y);
  277.     // Bitwise operators:
  278.     x = ~y;
  279.     x = x & y;
  280.     x = x | y;
  281.     x = x ^ y;
  282.     x = x << 1;
  283.     x = x >> 1;
  284.     x = x >>> 1;
  285.     // Compound assignment:
  286.     x += y;
  287.     x -= y;
  288.     x *= y;
  289.     x /= y;
  290.     x %= y;
  291.     x <<= 1;
  292.     x >>= 1;
  293.     x >>>= 1;
  294.     x &= y;
  295.     x ^= y;
  296.     x |= y;
  297.     // Casting:
  298.     //! boolean b = (boolean)x;
  299.     char c = (char)x;
  300.     byte B = (byte)x;
  301.     short s = (short)x;
  302.     long l = (long)x;
  303.     float f = (float)x;
  304.     double d = (double)x;
  305.   }
  306.   void longTest(long x, long y) {
  307.     // Arithmetic operators:
  308.     x = x * y;
  309.     x = x / y;
  310.     x = x % y;
  311.     x = x + y;
  312.     x = x - y;
  313.     x++;
  314.     x--;
  315.     x = +y;
  316.     x = -y;
  317.     // Relational and logical:
  318.     f(x > y);
  319.     f(x >= y);
  320.     f(x < y);
  321.     f(x <= y);
  322.     f(x == y);
  323.     f(x != y);
  324.     //! f(!x);
  325.     //! f(x && y);
  326.     //! f(x || y);
  327.     // Bitwise operators:
  328.     x = ~y;
  329.     x = x & y;
  330.     x = x | y;
  331.     x = x ^ y;
  332.     x = x << 1;
  333.     x = x >> 1;
  334.     x = x >>> 1;
  335.     // Compound assignment:
  336.     x += y;
  337.     x -= y;
  338.     x *= y;
  339.     x /= y;
  340.     x %= y;
  341.     x <<= 1;
  342.     x >>= 1;
  343.     x >>>= 1;
  344.     x &= y;
  345.     x ^= y;
  346.     x |= y;
  347.     // Casting:
  348.     //! boolean b = (boolean)x;
  349.     char c = (char)x;
  350.     byte B = (byte)x;
  351.     short s = (short)x;
  352.     int i = (int)x;
  353.     float f = (float)x;
  354.     double d = (double)x;
  355.   }
  356.   void floatTest(float x, float y) {
  357.     // Arithmetic operators:
  358.     x = x * y;
  359.     x = x / y;
  360.     x = x % y;
  361.     x = x + y;
  362.     x = x - y;
  363.     x++;
  364.     x--;
  365.     x = +y;
  366.     x = -y;
  367.     // Relational and logical:
  368.     f(x > y);
  369.     f(x >= y);
  370.     f(x < y);
  371.     f(x <= y);
  372.     f(x == y);
  373.     f(x != y);
  374.     //! f(!x);
  375.     //! f(x && y);
  376.     //! f(x || y);
  377.     // Bitwise operators:
  378.     //! x = ~y;
  379.     //! x = x & y;
  380.     //! x = x | y;
  381.     //! x = x ^ y;
  382.     //! x = x << 1;
  383.     //! x = x >> 1;
  384.     //! x = x >>> 1;
  385.     // Compound assignment:
  386.     x += y;
  387.     x -= y;
  388.     x *= y;
  389.     x /= y;
  390.     x %= y;
  391.     //! x <<= 1;
  392.     //! x >>= 1;
  393.     //! x >>>= 1;
  394.     //! x &= y;
  395.     //! x ^= y;
  396.     //! x |= y;
  397.     // Casting:
  398.     //! boolean b = (boolean)x;
  399.     char c = (char)x;
  400.     byte B = (byte)x;
  401.     short s = (short)x;
  402.     int i = (int)x;
  403.     long l = (long)x;
  404.     double d = (double)x;
  405.   }
  406.   void doubleTest(double x, double y) {
  407.     // Arithmetic operators:
  408.     x = x * y;
  409.     x = x / y;
  410.     x = x % y;
  411.     x = x + y;
  412.     x = x - y;
  413.     x++;
  414.     x--;
  415.     x = +y;
  416.     x = -y;
  417.     // Relational and logical:
  418.     f(x > y);
  419.     f(x >= y);
  420.     f(x < y);
  421.     f(x <= y);
  422.     f(x == y);
  423.     f(x != y);
  424.     //! f(!x);
  425.     //! f(x && y);
  426.     //! f(x || y);
  427.     // Bitwise operators:
  428.     //! x = ~y;
  429.     //! x = x & y;
  430.     //! x = x | y;
  431.     //! x = x ^ y;
  432.     //! x = x << 1;
  433.     //! x = x >> 1;
  434.     //! x = x >>> 1;
  435.     // Compound assignment:
  436.     x += y;
  437.     x -= y;
  438.     x *= y;
  439.     x /= y;
  440.     x %= y;
  441.     //! x <<= 1;
  442.     //! x >>= 1;
  443.     //! x >>>= 1;
  444.     //! x &= y;
  445.     //! x ^= y;
  446.     //! x |= y;
  447.     // Casting:
  448.     //! boolean b = (boolean)x;
  449.     char c = (char)x;
  450.     byte B = (byte)x;
  451.     short s = (short)x;
  452.     int i = (int)x;
  453.     long l = (long)x;
  454.     float f = (float)x;
  455.   }
  456. } ///:~